package com.hua.Aop;

import com.hua.common.ResponseCode;
import com.hua.domain.User;
import com.hua.exception.BusinessException;
import com.hua.util.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * ClassName:AopOfCheckIsLogin
 * Package:com.hua.Aop
 * Description:
 *
 * @data: 2022/10/19 10:35
 * @author:ChenJH
 */

@Aspect
@Component
@Slf4j
public class AopOfCheckIsLogin {

    @Resource
    private RedisCache redisCache;

    @Pointcut("@annotation(com.hua.anotation.AopPoint)")
    public void point(){
    }

    @Before("point()")
    public void checkIsLogin(){
        Integer userId = redisCache.getCacheObject("userId");
        if(userId==null){
            log.error("用户没有登录,无法访问");
            throw new BusinessException(ResponseCode.NOT_LOGIN);
        }
        User userInfo = redisCache.getCacheObject("userId:" + userId);
        if(userInfo==null){
            log.error("用户没有登录,无法访问");
            throw new BusinessException(ResponseCode.NOT_LOGIN);
        }
    }
}
